package com.lanhao.aot.spider.config;

import java.io.FileReader;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

//@Configuration
public class JavaScriptEngineConfig {
	
	@Bean
	public Invocable invocable() {
		Invocable invocable = null;
		try {
			String jsName = "17track";
			// 创建一个脚本引擎管理器
			ScriptEngineManager manager = new ScriptEngineManager();
			// 获取一个指定的名称的脚本引擎
			ScriptEngine engine = manager.getEngineByName("js");
			// 获取js文件所在目录的路径
			// String path = NetSpiderFor17Track.class.getResource("").getPath()+"/js/";
			String path = "D:/";
			engine.eval(new FileReader(path + jsName + ".js"));
			// 3.将引擎转换为Invocable，这样才可以掉用js的方法
			invocable = (Invocable) engine;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return invocable;
	}
}
